// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.geeke.handler.AuthorizeInterceptor;

/**
 * <p>
 * web 配置
 * </p>
 *
 * @author 瞄你个汪
 * @since 2024/6/7 22:25
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private AuthorizeInterceptor authorizeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizeInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/")
                .excludePathPatterns("/error")
                // .excludePathPatterns("/sys/attach/preview/**")//放行文件预览接口
                .excludePathPatterns("/upload/**")//放行文件访问静态资源url
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/v3/**", "/swagger-ui.html/**","/swagger-ui/**", "/doc.html/**","/api-docs-ext/**")//放行swagger接口文档地址
                .addPathPatterns("/**/**");
    }

    /**
     * 配置跨域
     * @param registry 跨域资源共享注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // 是否发送Cookie
                .allowCredentials(false)
                // 放行哪些原始域
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
